#include "StdAfx.h"
#include "QRichTextWidget.h"

#include "QNewTreeWidget.h"

QRichTextWidget* QRichTextWidget::ms_pInstance = NULL;

QRichTextWidget::QRichTextWidget(QWidget* parent, Qt::WindowFlags f)
	: QWidget(parent, f)
{
	ui.setupUi(this);

	m_pEditor = NULL;
	setWindowModality(Qt::ApplicationModal);

	connect(ui.colorBtn, SIGNAL(clicked()), this, SLOT(OnSelectColor()));

	ui.linkBox->addItem("rolename");
	ui.linkBox->addItem("sex");

	connect(ui.linkBox, SIGNAL(activated(const QString&)), this, SLOT(OnSelectLink(const QString&)));

	ui.typeBox->addItem("LINK_NPC");
	ui.typeBox->addItem("LINK_ITEM");

	connect(ui.insertBtn, SIGNAL(clicked()), this, SLOT(OnInsertLink()));

	connect(ui.actionColor, SIGNAL(triggered(bool)), this, SLOT(OnActionColor(bool)));
	connect(ui.actionTag, SIGNAL(triggered(bool)), this, SLOT(OnActionTag(bool)));
	connect(ui.actionLink, SIGNAL(triggered(bool)), this, SLOT(OnActionLink(bool)));
}

QRichTextWidget::~QRichTextWidget(void)
{
}

QRichTextWidget& QRichTextWidget::GetInstance(void)
{
	if (ms_pInstance == NULL)
	{
		ms_pInstance = new QRichTextWidget;
	}
	return *ms_pInstance;
}

bool QRichTextWidget::showText(QNewTreeWidget* pEditor, const QString& rText)
{
	QString ConvertedString;
	QRegExp linkReg("<h\\s+type\\s*=\\s*\"(\\w*)\"\\s+id\\s*=\\s*\"(\\d+)\"\\s+showpos\\s*=\\s*\"(\\d)\"\\s*>(.*)</h\\s*>");

	LOG_PROCESS_ERROR(pEditor);
	m_pEditor = pEditor;

	ConvertedString = "<html><body>" + rText + "</body></html>";
	ConvertedString = ConvertedString.replace("\n", "<br>");
	ConvertedString = ConvertedString.replace(QRegExp("<c\\s+value\\s*=\\s*\"[a-fA-F0-9]{2}([a-fA-F0-9]{6})\""), "<font color='#\\1'");
	ConvertedString = ConvertedString.replace(QRegExp("</c\\s*>"), "</font>");

	ConvertedString = ConvertedString.replace(QRegExp("<rolename\\s*/>"), "<a href=\"rolename\">ROLENAME</a>");
	ConvertedString = ConvertedString.replace(QRegExp("<sex\\s*/>"), "<a href=\"sex\">SEX</a>");

	linkReg.setMinimal(true);
	ConvertedString = ConvertedString.replace(linkReg, "<a href=\"type=&quot;\\1&quot; id=&quot;\\2&quot; showpos=&quot;0&quot;\">\\4</a>");

	ui.textEdit->setText(ConvertedString);

	ui.textEdit->setFocus();
	show();

	return true;
Exit0:
	return false;
}

void QRichTextWidget::closeEvent(QCloseEvent* event)
{
	QString out;
	QColor lastColor(Qt::black);
	QString lastLink;

	LOG_PROCESS_ERROR(m_pEditor);

	QTreeWidgetItem* pItem = m_pEditor->currentItem();
	LOG_PROCESS_ERROR(pItem);

	QTextDocument* pTextDoc = ui.textEdit->document();
	LOG_PROCESS_ERROR(pTextDoc);

	for (QTextBlock block = pTextDoc->begin(); block != pTextDoc->end(); block = block.next())
	{
		const QTextCharFormat blockFormat = block.charFormat();

		for (QTextBlock::iterator it = block.begin(); !(it.atEnd()); ++it)
		{
			QTextFragment fragment = it.fragment();
			const QTextCharFormat format = fragment.charFormat();
			QColor color = format.foreground().color();
			const QString href = format.anchorHref();
			Qt::GlobalColor defaultColor = Qt::black;

			if (format.isAnchor())
			{
				if (href == "rolename" || href == "sex")
				{
					out += "<" + href + "/>";
					continue;
				}
				Qt::GlobalColor defaultColor = Qt::blue;
			}
			
			if (color != lastColor)
			{
				if (lastColor != QColor(defaultColor))
					out += "</c>";
			}

			if (href != lastLink)
			{
				if (lastLink != "")
					out += "</h>";
			}

			if (href != lastLink)
			{
				if (href != "")
					out += "<h " + href + ">"; 
			}

			if (color != lastColor)
			{
				if (color != QColor(defaultColor))
				{
					char szColor[16];
					sprintf(szColor, "%02X%02X%02X", color.red(), color.green(), color.blue());

					out += QString("<c value=\"FF") + szColor + "\">";
				}

			}

			out += fragment.text();

			lastColor = color;
			lastLink = href;
		}

		out += "\n";
	}

	out.chop(1);

	if (lastColor != QColor(Qt::black) && lastColor != QColor(Qt::blue))
		out += "</c>";

	if (lastLink != "")
		out += "</h>";

	out.replace(QChar(0x2028), QString("\n"));
	pItem->setText(1, out);

	m_pEditor->setFocus(Qt::ShortcutFocusReason);

Exit0:
	return;
}

void QRichTextWidget::OnSelectColor(void)
{
	QColor color = QColorDialog::getColor(QColor(Qt::black), this, "Select Text Color");
	QTextCharFormat format = ui.textEdit->textCursor().charFormat();
	format.setForeground(QBrush(color));
	ui.textEdit->textCursor().setCharFormat(format);
	ui.textEdit->setFocus();
}

void QRichTextWidget::OnSelectLink(const QString& text)
{
	ui.textEdit->textCursor().insertHtml("<a href=\"" + text + "\">" + text.toUpper() + "</a>");
	ui.textEdit->setFocus();
}

void QRichTextWidget::OnInsertLink(void)
{
	ui.textEdit->textCursor().insertHtml(
		"<a href=\"type=&quot;" + ui.typeBox->currentText() + 
		"&quot; id=&quot;" + ui.idEdit->text() + "&quot; showpos=&quot;0&quot;\">" + 
		ui.linkText->text() + "</a>");
	ui.textEdit->setFocus();
}

void QRichTextWidget::OnActionColor(bool checked)
{
	ui.colorBtn->click();
}

void QRichTextWidget::OnActionTag(bool checked)
{
	ui.linkBox->setFocus();
	ui.linkBox->showPopup();
}

void QRichTextWidget::OnActionLink(bool checked)
{
	ui.typeBox->setFocus();
	ui.typeBox->showPopup();
}
